// vite.config.js
import { resolve } from "path";
import { defineConfig, normalizePath } from "vite";
import { viteStaticCopy } from "vite-plugin-static-copy";
const project = "networkCustom"; //!注意切换项目
export default defineConfig(({ command, mode, ssrBuild }) => {
  let config = {
    build: {
      minify: false,
      emptyOutDir: false,
      lib: {
        entry: resolve(__dirname, project, "src", "index.ts"),
        formats: ["cjs"],
        fileName: "main",
      },
      commonjsOptions: {
        defaultIsModuleExports: true,
      },
      rollupOptions: {
        input: {
          main: resolve(__dirname, project, "src", "index.ts"),
        },
        output: {
          name: "main",
          format: "commonjs",
          esModule: "if-default-prop",
          manualChunks: undefined,
          dir: resolve(__dirname, project),
        },
        external: ["siyuan"],
      },
      //构建后是否生成 source map 文件
      sourcemap: false,
    },
  };
  if (mode == "development") {
    return config;
  }
  if (mode == "production") {
    config.build.minify = true;
    config.build.emptyOutDir = true;
    config.build.rollupOptions.output.dir = resolve(__dirname, project, "dist");
    let outpath = normalizePath(resolve(__dirname, project, "dist"));
    config.plugins = [
      viteStaticCopy({
        targets: [
          {
            src: normalizePath(resolve(__dirname, project, "manifest.json")),
            dest: outpath,
          },
          {
            src: normalizePath(resolve(__dirname, project, "config.json")),
            dest: outpath,
          },
        ],
      }),
    ];
    return config;
  }
});
